package com.zhiyume.aop;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class LogAspect {
	private final Logger log = Logger.getLogger(getClass());

	 // service层的且切点
    @Pointcut("execution(* com.zhiyume.*.*(..))")
    public void serviceAspect() {

    }
 // 前置通知用于拦截service层的日志
    @Before("serviceAspect()")
    public void doAfter(JoinPoint joinPoint) throws Exception {
        // 获取包括包名在内的全方法名
//        String allmethodName = joinPoint.getTarget().getClass().getName() + "."
//                + joinPoint.getSignature().getName() + "()";

        // 获取请求的方法
        String requstMethodName = joinPoint.getSignature().getName() + "()";
        // 判断如果是查询方法就不记录到数据库里面insert update delete
//        String methodName = requstMethodName.substring(0, 6);
        String classType = joinPoint.getTarget().getClass().getName();
        Class<?> clazz = Class.forName(classType);
        Object[] objs = joinPoint.getArgs();
        // requstMethodName.startsWith(prefix, 0)
//        methodName.equals("insert") || methodName.equals("create")
//        || methodName.equals("save") || methodName.equals("add")
        if (requstMethodName.startsWith("insert") || requstMethodName.startsWith("create")
            || requstMethodName.startsWith("save") || requstMethodName.startsWith("add")) {
            log.info("模块名字：管理中心  serviceName: " + clazz.getName() + ", methodName: "
                    + requstMethodName);//记录操作的模块，方法
            log.info("增加数据json串为＝＝＝"+JSON.toJSON(objs));
            
        }else if(requstMethodName.startsWith("update") || requstMethodName.startsWith("edit")){
        	 log.info("模块名字：管理中心  serviceName: " + clazz.getName() + ", methodName: "
                     + requstMethodName);//记录操作的模块，方法
             log.info("修改数据json串为＝＝＝"+JSON.toJSON(objs));
        }else if(requstMethodName.startsWith("delete")){
        	log.info("模块名字：管理中心  serviceName: " + clazz.getName() + ", methodName: "
                    + requstMethodName);//记录操作的模块，方法
            log.info("删除数据主键id json串为＝＝＝"+JSON.toJSON(objs));
        }else{
        	
        }

    }

    /**
     * 获取注解中对方法的描述信息 用于service层注解
     * 
     * @throws ClassNotFoundException
     */
    public static String getServiceMethodDescription(JoinPoint joinPoint)
            throws ClassNotFoundException {
        // String targetName = .getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = joinPoint.getTarget().getClass();// Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        String description = "";
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == arguments.length) {
                    if (method.getAnnotation(Servicelog.class) != null) {

                        description = method.getAnnotation(
                                Servicelog.class).description();
                        if (description == null) {
                            description = "";
                        }
                        break;
                    }
                }
            }
        }
        return description;

    }
}
